package com.lucweb.action;

import com.lucweb.entity.*;

import com.lucweb.service.BaseDictService;
import com.lucweb.service.QuestionService;
import com.lucweb.service.WhyjService;

import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller("whyjAction")
@Scope("prototype")
public class WhyjAction {
    @Resource
    private WhyjService whyjService;

    @Resource
    private QuestionService questionService;

    @Resource
    private BaseDictService baseDictService;

    private String company;
    private String type;
    private String timeqi;
    private String timezhi;


    private static final long serialVersionUID = 1L;

    private BaseDict baseDict;

    private TaskWhyj taskWhyj;


    private TaskWhyjtz taskWhyjtz;

    private List<TaskWhyjtz> WhyjtzList=new ArrayList<TaskWhyjtz>();

    private List<TaskWhyjtz> taskWhyjtzList=new ArrayList<TaskWhyjtz>();

    private int taskId;

    private File file;
    //文件名称
    private String fileFileName;

    //文件类型
    private String fileContentType;
    //注意：文件名称和文件类型的名称前缀必须相同，

    private InputStream fileInput;

    private String fileName;
    private String inputName;
    private String contentDispostion;


    //单体名称
    private String tzdtName;
    //执行时间
    private String exeTime;

    public TaskQuestion getTaskQuestion() {
        return taskQuestion;
    }

    public void setTaskQuestion(TaskQuestion taskQuestion) {
        this.taskQuestion = taskQuestion;
    }

    public List<TaskQuestion> getQuestionList1() {
        return questionList1;
    }

    public void setQuestionList1(List<TaskQuestion> questionList1) {
        this.questionList1 = questionList1;
    }

    private TaskQuestion taskQuestion;

    private List<TaskQuestion>questionList1 = new ArrayList<TaskQuestion>();

    public List<TaskQuestion> getQuestionList2() {
        return questionList2;
    }

    public void setQuestionList2(List<TaskQuestion> questionList2) {
        this.questionList2 = questionList2;
    }

    private List<TaskQuestion>questionList2 = new ArrayList<TaskQuestion>();

    //新建检测任务方法
    public String newWhyjTask() {

        String number =  whyjService.newWhyjTask();

        List<BaseDict> nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");

        ActionContext.getContext().put("number", number);
        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("companyList",companyList);


        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and isfinish != '已处理' and isSelected='1' and (type = '维护' or type = '维护遗留' )");

        List<TaskQuestion>questionList2 = questionService.getQuestion("companyName = '"+company+"' " +
                "and isfinish != '已处理' and (isSelected !='1' or isSelected is Null) and (type = '维护' or type = '维护遗留' )");


        System.out.println(questionList2);


        ActionContext.getContext().put("questionList1", questionListAll);
        ActionContext.getContext().put("questionList2", questionList2);
        ActionContext.getContext().put("company", company);



        return "number";

    }

//    //保存台账
//    public String saveTz() {
//
//        System.out.println(WhyjtzList);
//
//        for(int i=0; i<WhyjtzList.size();i++){
//
//
//            if(WhyjtzList.get(i)!=null){
//                if (WhyjtzList.get(i).getIschecked().equals("1")){
//                    taskWhyjtz=WhyjtzList.get(i);
//                    System.out.println(taskWhyjtz);
//                    taskWhyjtz.setId(UUID.randomUUID().toString());
//                    whyjService.saveTz(taskWhyjtz);
//                }
//
//            }
//
//
//        }
//
//        return "member";
//
//    }
    //保存台账
    public String finishTz() {

        for(int i=0; i<taskWhyjtzList.size();i++){
            taskWhyjtz=taskWhyjtzList.get(i);

            whyjService.saveTz(taskWhyjtz);

        }

        List<TaskWhyjtz>taskWhyjtzList=whyjService.showTzFinish("'"+company+"' and tzdtName='"+tzdtName+"'");

        ActionContext.getContext().put("taskWhyjtzList",taskWhyjtzList);
        ActionContext.getContext().put("company",company);
        ActionContext.getContext().put("exeTime",exeTime);
        ActionContext.getContext().put("tzdtName",tzdtName);

        return "print";

    }


    //打开50选10台账
    public String showTzList() {

       List<TaskWhyjtz>taskWhyjtzList=whyjService.showTzList("'"+company+"'"+" and tzdtName='"+tzdtName+"'");

       ActionContext.getContext().put("taskWhyjtzList",taskWhyjtzList);
       ActionContext.getContext().put("company",company);
       ActionContext.getContext().put("tzdtName",tzdtName);


        return "yjcTz";

    }

    //显示台账单体详细列表
    public String showTzDtList() {

        List<TaskWhyjtz>companyDtList=whyjService.showTzList("'"+company+"'"+" and tzdtName='"+tzdtName+"'");

        ActionContext.getContext().put("companyDtList",companyDtList);
        ActionContext.getContext().put("company",company);


        return "yjcTzDt";

    }

    //显示台账单体详细列表
    public String showTzDt() {

        List<String>DtList=whyjService.showDtList("companyName='"+company+"'");

        ActionContext.getContext().put("DtList",DtList);
        ActionContext.getContext().put("company",company);


        return "yjcTzDtIndex";

    }

    //删除台账单体
    public String deleteTzDt() {

        whyjService.deleteTz("companyName='"+company+"' and tzdtName='"+tzdtName+"'");

        List<String>DtList=whyjService.showDtList("companyName='"+company+"'");

        ActionContext.getContext().put("DtList",DtList);
        ActionContext.getContext().put("company",company);


        return "yjcTzDtIndex";

    }


    //新建台账单体
    public String NewTzDt() {

        List<TaskTz>tzList=whyjService.showTz();
        ActionContext.getContext().put("tzList",tzList);

        ActionContext.getContext().put("company",company);

        return "yjcTzDt";

    }

    //保存台账单体转发到indexdt页面
    public String SaveTzDt() {

        for(int i=0; i<WhyjtzList.size();i++){

            if(WhyjtzList.get(i)!=null){
                if (WhyjtzList.get(i).getIschecked().equals("1")){
                    taskWhyjtz=WhyjtzList.get(i);
                    taskWhyjtz.setTzdtName(tzdtName);
                    taskWhyjtz.setCompanyName(company);
                    System.out.println(taskWhyjtz);
                    taskWhyjtz.setId(UUID.randomUUID().toString());
                    whyjService.saveTz(taskWhyjtz);

                }

            }

        }

        List<String>DtList=whyjService.showDtList("companyName='"+company+"'");

        ActionContext.getContext().put("DtList",DtList);
        ActionContext.getContext().put("company",company);


        return "yjcTzDtIndex";

    }


    //打印
    public String printWhyjTask(){

        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }
        for(int i=0; i<questionList2.size();i++){
            taskQuestion=questionList2.get(i);

            questionService.addQuestion(taskQuestion);

        }


        List<TaskQuestion>questionList = questionService.getQuestion("companyName = '"+taskWhyj.getCompanyName()+"' " +
                "and isfinish != '已处理' and isSelected='1' and (type = '维护' or type = '维护遗留' )");

        ActionContext.getContext().put("taskWhyj", taskWhyj);
        ActionContext.getContext().put("questionList", questionList);



        return "printWhyj";
    }

    //打印
    public String printwhfk(){

        List<TaskWhyj>taskWhyjList=whyjService.showTaskByName("companyName = '"+company+"' " +
                "and executeTime >= '"+timeqi+"' and executeTime <= '"+timezhi+"'");

        int count = taskWhyjList.size();

        ActionContext.getContext().put("count", count);
        ActionContext.getContext().put("taskWhyjList", taskWhyjList);



        return "printwhfk";
    }


    //修改问题
    public String editWhyjTask(){


        taskWhyj = whyjService.getWhyjtaskById(taskId);

        ActionContext.getContext().put("taskWhyj", taskWhyj);

        List<BaseDict>nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");

        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("companyList",companyList);

        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and isfinish != '已处理' and isSelected ='1' and (type = '维护' or type = '维护遗留' )");

        List<TaskQuestion>questionList2 = questionService.getQuestion("companyName = '"+company+"' " +
                "and isfinish != '已处理' and (isSelected !='1' or isSelected is Null) and (type = '维护' or type = '维护遗留' )");


        ActionContext.getContext().put("questionList1", questionListAll);
        ActionContext.getContext().put("questionList2", questionList2);
        ActionContext.getContext().put("company", company);


        System.out.println(taskWhyj);

        return "number";
    }

    //删除任务
    public String deleteWhyjTask(){

        boolean flag = whyjService.deleteWhyjtask(taskId);


        return "number";
    }

    //新建检测任务方法
    public String saveOrUpdate() throws IOException {

        //获取需要上传文件的文件路径
        File uploadFile=new File(ServletActionContext.getServletContext().getRealPath("uploadFile"));
        //判断文件是否上传，如果上传的话将会创建该目录
        if(!uploadFile.exists()){
            uploadFile.mkdir(); //创建该目录
        }


        try {
            if(file!=null){
                FileUtils.copyFile(file,new File(uploadFile,fileFileName));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }





        if(taskWhyj.getUpdown().length()==0){
            if(fileFileName!=null){
                taskWhyj.setUpdown(fileFileName);
            }else {
                taskWhyj.setUpdown(null);
            }

        }else {
            if(fileFileName!=null){
                taskWhyj.setUpdown(fileFileName);
            }

        }


        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }
        for(int i=0; i<questionList2.size();i++){
            taskQuestion=questionList2.get(i);

            questionService.addQuestion(taskQuestion);

        }

        System.out.println(taskWhyj);
        whyjService.saveOrUpdate(taskWhyj);

        return "success";

    }





    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public TaskWhyjtz getTaskWhyjtz() {
        return taskWhyjtz;
    }

    public void setTaskWhyjtz(TaskWhyjtz taskWhyjtz) {
        this.taskWhyjtz = taskWhyjtz;
    }

    public List<TaskWhyjtz> getWhyjtzList() {
        return WhyjtzList;
    }

    public void setWhyjtzList(List<TaskWhyjtz> whyjtzList) {
        WhyjtzList = whyjtzList;
    }

    public WhyjService getWhyjService() {
        return whyjService;
    }

    public void setWhyjService(WhyjService whyjService) {
        this.whyjService = whyjService;
    }

    public BaseDictService getBaseDictService() {
        return baseDictService;
    }

    public void setBaseDictService(BaseDictService baseDictService) {
        this.baseDictService = baseDictService;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public BaseDict getBaseDict() {
        return baseDict;
    }

    public void setBaseDict(BaseDict baseDict) {
        this.baseDict = baseDict;
    }

    public TaskWhyj getTaskWhyj() {
        return taskWhyj;
    }

    public void setTaskWhyj(TaskWhyj taskWhyj) {
        this.taskWhyj = taskWhyj;
    }

    public int getTaskId() {
        return taskId;
    }

    public void setTaskId(int taskId) {
        this.taskId = taskId;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public InputStream getFileInput() {
        return fileInput;
    }

    public void setFileInput(InputStream fileInput) {
        this.fileInput = fileInput;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getInputName() {
        return inputName;
    }

    public void setInputName(String inputName) {
        this.inputName = inputName;
    }

    public String getContentDispostion() {
        return contentDispostion;
    }

    public void setContentDispostion(String contentDispostion) {
        this.contentDispostion = contentDispostion;
    }

    public List<TaskWhyjtz> getTaskWhyjtzList() {
        return taskWhyjtzList;
    }

    public void setTaskWhyjtzList(List<TaskWhyjtz> taskWhyjtzList) {
        this.taskWhyjtzList = taskWhyjtzList;
    }

    public String getTimeqi() {
        return timeqi;
    }

    public void setTimeqi(String timeqi) {
        this.timeqi = timeqi;
    }

    public String getTimezhi() {
        return timezhi;
    }

    public void setTimezhi(String timezhi) {
        this.timezhi = timezhi;
    }

    public String getTzdtName() {
        return tzdtName;
    }

    public void setTzdtName(String tzdtName) {
        this.tzdtName = tzdtName;
    }

    public String getExeTime() {
        return exeTime;
    }

    public void setExeTime(String exeTime) {
        this.exeTime = exeTime;
    }
}
